﻿(function() {
    var blogService = angular.module('blogService', ['infinite-scroll']);
    blogService.factory('microblogsService',
        function($http) {
            var blogfactory = {};

            /*
            * 获取普通微博数据
            * startId:当前页最后一项的id
            * page:页索引
            */
            blogfactory.getMicroBlogs = function(startId, page) {
                return $http({
                    method: 'POST',
                    url: '/Social/Home/GetNewMicroBlogs',
                    data: { startId: startId, pageIndex: page }

                });
            };


            /*
             * 获取微博数据
             * type:微博数据来源类型
             * startId:当前页最后一项的id
             * page:页索引
             * time:24h or 48h
             */
            blogfactory.getBlogs = function(type, startId, page, time) {
                var postUrl;
                switch (type) {
                case 0: //关注动态
                    postUrl = '/Social/Home/GetNewMicroBlogs';
                    break;
                case 1: //朋友圈
                    postUrl = '/Social/Friends/GetFriendMicroBlogs';
                    break;
                case 2: //我的微博
                    postUrl = '/Social/Home/GetBlogList';
                    break;
                case 3: //我的收藏
                    postUrl = '/Social/Collection/GetCollectionList';
                    break;
                default:
                    postUrl = '/Social/Home/GetHotMicroBlogs';
                    break;
                }
                return $http({
                    method: 'POST',
                    url: postUrl,
                    data: { rcount: startId, startId: startId, pageIndex: page, time: time, pageSize: 15 }

                });
            };

            /**
             * 微博搜索
             * @param {} keyWord 
             * @param {} startId 
             * @param {} pageIndex 
             * @returns {} 
             */
            blogfactory.searchBlog = function(keyWord, startId, pageIndex) {
                return $http({
                    method: 'POST',
                    url: '/Social/Blog/SearchBlogs',
                    data: { keyWord: keyWord, startId: startId, pageIndex: pageIndex, pageSize: 15 }
                });
            }

            /**
             * 获取交易员的微博列表
             * @param {} traderId 
             * @param {} startId 
             * @param {} page 
             * @returns {} 
             */
            blogfactory.getTraderBlogList = function(traderId, startId, page) {
                return $http({
                    method: 'POST',
                    url: '/Member/Trader/GetTraderBlogList',
                    data: { traderId: traderId, startId: startId, pageIndex: page }

                });
            }

            /**
             * 置顶微博
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.setTopBlog = function(uId, bid, isCancel, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Member/Trader/SetTopBlogId',
                            data: { userId: uId, blogId: bid, isCancel: isCancel }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }

            /**
             * 获取我的关注列表
             * @param {} page 
             * @param {} type 
             * @returns {} 
             */
            blogfactory.getFollowerList = function(page, type) {
                return $http({
                    method: 'POST',
                    url: '/Social/Attention/GetFollowerList',
                    data: { pageIndex: page, pageSize: 15, type: type }

                });
            }

            /**
         * 获取用户关注列表
         * @param {} page 
         * @param {} type 
         * @returns {} 
         */
            blogfactory.getUserFollowerList = function(page, type, userId) {
                return $http({
                    method: 'POST',
                    url: '/Social/Attention/GetFollowerList',
                    data: { pageIndex: page, pageSize: 15, type: type, userId: userId }

                });
            }

            /**
             * 获取粉丝
             * @param {} page 
             * @returns {} 
             */
            blogfactory.getBeFolloweredList = function(page) {
                return $http({
                    method: 'POST',
                    url: '/Social/Attention/GetBeFolloweredList',
                    data: { pageIndex: page, pageSize: 15 }

                });
            }


            /**
             * 获取用户粉丝
             * @param {} page 
             * @returns {} 
             */
            blogfactory.getUserBeFolloweredList = function(page, userId) {
                return $http({
                    method: 'POST',
                    url: '/Social/Attention/GetBeFolloweredList',
                    data: { pageIndex: page, pageSize: 15, userId: userId }

                });
            }


            /**
           * 发布微博
           * @param {} blogBody 
           * @param {} imgIds 
           * @returns {} 
           */
            blogfactory.reportMicroBlog = function(blogBody, imgIds, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/ReportBlog',
                            data: { blogBady: blogBody, imgIds: imgIds }
                        })
                        .success(function(response) {
                            callback(response);
                        });

                })

            };

            /**
             * 根据微博ID获取长微博对象
             * @param {} rcount 
             * @param {} page 
             * @param {} time 
             * @returns {} 
             */
            blogfactory.getLongBlogBodyById = function(blogId, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Blog/GetLongBlogBodyById',
                        data: { id: blogId }
                    })
                    .success(function(response) {
                        callback(response);
                    });

            };


            /**
             * 关注用户
             * @param {用户ID} uid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.AttentionUser = function(uid, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/AttentionOrNo',
                            data: { id: uid }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }

            /**
             * 收藏或者取消收藏
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.collectMicroBlog = function(bid, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/CollectMicroBlog',
                            data: { id: bid }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }


            /**
             * 删除微博
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.delMicroBlog = function(bid, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/DelMicroBlog',
                            data: { id: bid }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });


            }


            /**
             * 微博点赞或取消赞
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.praiseMicroBlog = function(bid, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/PraiseMicroBlog',
                            data: { id: bid }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }


            /**
             * 点赞评论
             * @param {} 评论对象ID 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.praiseComment = function(objectId, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/PraiseComment',
                            data: { id: objectId }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });
            }

            /**
             * 删除评论
             * @param {} cid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.delComment = function(cid, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Comment/DeleteComment',
                            data: { id: cid }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }

            /**
             * 添加评论
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.addComment = function(bid, combody, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/CommentMicroBlog',
                            data: { blogId: bid, combody: combody }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });
            }

            /**
             * 回复评论
             * @param {} bid 评论列表所在的微博ID
             * @param {} cid 待回复的评论ID
             * @param {} parentUserId 待回复评论所属的UserId
             * @param {} recombody 回复内容
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.reComment = function(bid, cid, parentUserId, recombody, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/ReCommentMicroBlog',
                            data: { reblogId: bid, parentId: cid, parentUserId: parentUserId, recombody: recombody }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });

            }


            /**
             * 获取关注好友列表
             * @param {} key 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.getFollowers = function(key, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Blog/GetFollowerList',
                        data: { key: key }
                    })
                    .success(function(response) {
                        callback(response);
                    });
            }


            /**
             * 加载评论列表分页数据
             * @param {} bid 
             * @param {} page 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.loadCommentsByBlogId = function(bid, page, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Blog/LoadCommentsByBlogId',
                        data: { id: bid, pageIndex: page, pageSize: 10 }
                    })
                    .success(function(response) {
                        callback(response);
                    });

            }

            /**
             * 根据微博id获取微博对象
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.getBlogByBlogId = function(bid, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Blog/SelectBlog',
                        data: { blogID: bid }
                    })
                    .success(function(response) {
                        callback(response);
                    });

            }

            /**
             * 获取该微博被转发的列表数据
             * @param {} bid 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.getForwarBlogList = function(blogID, page, pageSize, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Blog/GetForwarBlogList',
                        data: { blogId: blogID, pageIndex: page, pageSize: pageSize }
                    })
                    .success(function(response) {
                        callback(response);
                    });

            }

            /**
             * 转发微博
             * @param {} blogId 
             * @param {} 转发内容 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.forwardMicroBlog = function(blogId, forwardContent, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/ForwarMicroBlog',
                            data: {
                                blogId: blogId,
                                forbody: forwardContent
                            }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });


            }

            /**
             * 发送私信
             * @param {} 目标用户UserId
             * @param {} 私信内容 
             * @param {} 图片ID 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.sendPrivateLetter = function(inUserid, letterContent, imgId, callback) {
                requiredLogin(function() {
                    $http({
                            method: 'POST',
                            url: '/Social/Blog/SendPrivateMsg',
                            data: {
                                inUserid: inUserid,
                                msgBody: letterContent,
                                imgIds: imgId,
                                pageType: 1
                            }
                        })
                        .success(function(response) {
                            callback(response);
                        });
                });


            }


            /**
             * 获取今日关注列表数据
             * @param {} 内容类型 
             * @param {} 推荐位 
             * @param {Number} width 
             * @param {Number} height 
             * @param {} 取前多少条数据 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.getTodayAttention = function(contentType, recPosition, width, height, top, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/Home/GetTodayAttentionJsonList',
                        data: {
                            contentType: contentType,
                            recPosition: recPosition,
                            w: width,
                            h: height,
                            top: top
                        }
                    })
                    .success(function(response) {
                        callback(response);
                    });

            }

            /**
             * 更新微博阅读数量
             * @param {} id 
             * @param {} callback 
             * @returns {} 
             */
            blogfactory.updateBlogViewCount = function(id, callback) {
                $http({
                        method: 'POST',
                        url: '/Social/BlogDetail/UpdateBlogViewCount',
                        data: {
                            id: id
                        }
                    })
                    .success(function(response) {
                        callback(response);
                    });
            }


            return blogfactory;

        });
}());